{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ensure dependencies are installed. If itkwidgets was not installed, reload the browser tab after installation.\n",
    "import sys\n",
    "!{sys.executable} -m pip install itk-parabolicmorphology itkwidgets ipympl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib.request import urlretrieve\n",
    "import os\n",
    "import itk\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "from itkwidgets import view, compare, cm, line_profile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_name = 'cthead1.png'\n",
    "if not os.path.exists(file_name):\n",
    "    url = 'https://data.kitware.com/api/v1/file/57b5d9ef8d777f10f269538f/download'\n",
    "    urlretrieve(url, file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e5703d1194f84f9daeb85a8b254b3d94",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=False, description='cmap'), Checkbox(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cthead1 = itk.imread(file_name)\n",
    "\n",
    "closed = itk.parabolic_close_image_filter(cthead1, scale=0.2)\n",
    "\n",
    "compare(cthead1, closed, cmap=cm.gray, ui_collapsed=True, annotations=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cc6ba2f025024eb8b67e2ed0b0cd3961",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=False, description='cmap'), Checkbox(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "opened = itk.parabolic_open_image_filter(cthead1, scale=0.2)\n",
    "\n",
    "compare(cthead1, opened, cmap=cm.gray, ui_collapsed=True, annotations=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2bdbe879e59442d19c2c41532f485254",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=False, description='cmap'), Checkbox(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eroded = itk.parabolic_erode_image_filter(cthead1, scale=0.2)\n",
    "\n",
    "compare(cthead1, eroded, cmap=cm.gray, ui_collapsed=True, annotations=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fd9565fc11b84f108f757dd2c9ef3e87",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=False, description='cmap'), Checkbox(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dilated = itk.parabolic_dilate_image_filter(cthead1, scale=0.2)\n",
    "\n",
    "compare(cthead1, dilated, cmap=cm.gray, ui_collapsed=True, annotations=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4315bdaf6c3d4ac49fc7d8cb8510ad01",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=False, description='cmap'), Checkbox(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sharpened = itk.morphological_sharpening_image_filter(cthead1, scale=0.2)\n",
    "\n",
    "compare(cthead1, sharpened, cmap=cm.gray, ui_collapsed=True, annotations=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8cef5544ebdf4d879d0344c97a17a2b6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(LineProfiler(mode='z', rendered_image=<itk.itkImagePython.itkImageUC2; proxy of <Swig Object of…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Click on two points in the displayed image to define the line\n",
    "line_profile(cthead1, comparisons={'Dilated': dilated,\n",
    "                                   'Eroded': eroded,\n",
    "                                   'Opened': opened,\n",
    "                                   'Closed': closed,\n",
    "                                   'Sharpened': sharpened })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
